package com.example.kotlin.ui

import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.example.kotlin.MyDBUtil
import com.example.kotlin.R
import com.example.kotlin.Student
import com.example.kotlin.utils.OnClick
import com.example.kotlin.utils.ViewById
import com.example.kotlin.utils.ViewUtils

class GreendaoActivity : AppCompatActivity() {
    var studentDao = MyDBUtil.getInstance().daoSession.studentDao

    @ViewById(R.id.btn_show)
    var show: Button? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_greendao)
        ViewUtils.inject(this)
        // ButterKnife.bind(this);
    }

    @OnClick(R.id.btn_delete)
    fun delete() {
        val daoSession = MyDBUtil.getInstance().daoSession
        daoSession.deleteAll(Student::class.java)
    }

    @OnClick(R.id.btn_update)
    fun update() {
        val daoSession = MyDBUtil.getInstance().getDaoSession();
        val list = studentDao.queryBuilder().list()
        if (list.size > 0) {

            val student: Student = list.first()
            student.name = "小芳宝"
            student.sex = "女"
            student.age = 28
            daoSession.update(student)
        }
    }

    @OnClick(R.id.btn_add)
    fun insert() {
        val daoSession = MyDBUtil.getInstance().daoSession
        val student = Student()
        val l = System.currentTimeMillis() % 2
        student.age = if (l % 2 == 0L) 18 else 30
        student.telPhone = "39971708"
        val chineseName = "陈梦佳2"
        student.name = chineseName
        student.studentNo = 12
        if (l % 2 == 0L) {
            student.sex = "男"
        } else {
            student.sex = "女"
        }
        //        student.setAddress(RandomValue.getRoad());
//        student.setGrade(String.valueOf(age % 10) + "年纪");
//        student.setSchoolName(RandomValue.getSchoolName());
        daoSession.insert(student)
    }

    @OnClick(R.id.btn_query)
    fun query() {
        //  List<Student> list = studentDao.queryBuilder().list();
//        for (Student student : list) {
//
//        }
        val daoSession = MyDBUtil.getInstance().daoSession
        val studentQueryBuilder = daoSession.queryBuilder(Student::class.java)
        val list = studentQueryBuilder.list()
        show?.text = list.toString()

    }
}